﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Enumerations.Channel_Enum;

public partial class Master_Admin_ModifyUser : Admin_BasePage
{
    protected AspNetCs.Model.Users_Model model = new Users_Model();
    protected Users bll = User_Bll.CreateNew;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            initUserInfo();
        }
        else 
        {
            model = ViewState["oldModel"] as Users_Model;
        }
    }

    protected void initUserInfo()
    {
        if(!string.IsNullOrEmpty(Request["id"]))
        {
            int Userid = StringHelper.isNumber_int(Request["id"]);
            model = bll.GetModel(Userid);
            ViewState["oldModel"] = model;
            if(model.UserId>0)
            {
                lblUserName.Text = model.UserName;
                txtEmail.Text = model.UserEmail;
                txtMsn.Text = model.Msn;
                txtMyWeb.Text = model.HomePage;
                //txtPwd.Text = model.Pwd;
                txtPwdAnswer.Text = model.Answer;
                txtPwdQuestion.Text = model.Quesion;
                txtQQ.Text = model.QQ;
                txtNum.Text = model.UserMobile;
                
                EnumDescription[] Sex_Ed = EnumDescription.GetFieldTexts(typeof(Sex_Enum), EnumDescription.SortType.Rank);
                this.rbtnSex.DataTextField = "EnumDisplayText";
                this.rbtnSex.DataValueField = "EnumDisplayText";
                this.rbtnSex.DataSource = Sex_Ed;
                this.rbtnSex.DataBind();
                this.rbtnSex.SelectedValue = model.Sex;

                EnumDescription[] State_Ed = EnumDescription.GetFieldTexts(typeof(LockUserEnum), EnumDescription.SortType.Rank);
                this.rbtnState.DataTextField = "EnumDisplayText";
                this.rbtnState.DataValueField = "EnumValue";
                this.rbtnState.DataSource = State_Ed;
                this.rbtnState.DataBind();
                this.rbtnState.SelectedValue = model.LockUser.ToString();

                
            }
        }
        
    }


    protected void btnAdd_Click(object sender,EventArgs e)
    {
        SaveUserInfo();
    }


    public void SaveUserInfo()
    {
        Users_Model UserModel = new Users_Model();
        UserModel.UserEmail=txtEmail.Text ;
        UserModel.Msn =txtMsn.Text;
        UserModel.HomePage= txtMyWeb.Text;
        UserModel.Answer= txtPwdAnswer.Text;
        UserModel.Quesion=txtPwdQuestion.Text ;
        UserModel.QQ=txtQQ.Text ;
        UserModel.Birthday = this.model.Birthday;
        UserModel.UserMobile = txtNum.Text;
        UserModel.Sex = rbtnSex.SelectedValue;
        UserModel.UserId = this.model.UserId;

        if (txtPwd.Text == "")
        {
            UserModel.Pwd =EncryptString.md5(txtPwd.Text);
        }
        else
        {
            UserModel.Pwd = this.model.Pwd;
        }
        UserModel.LockUser = StringHelper.isNumber_int(rbtnState.SelectedValue);
        UserModel.AddTime = this.model.AddTime;
        UserModel.Article = this.model.Article;

        if (Request["selProvice"] != null)
        {
            UserModel.Province = StringHelper.isNumber_int(Request["selProvice"].ToString());

        }
        if (model.Province > 0)
        {
            if (Request["selCity"] != null)
            {
                UserModel.City = StringHelper.isNumber_int(Request["selCity"].ToString());
            }
        }
        UserModel.Readme = this.model.Readme;
        UserModel.UserDel = this.model.UserDel;
        UserModel.UserPhoto = this.model.UserPhoto;
        bll.AdminUpdate(UserModel);

        ResultStr("Admin_ManageUsers.aspx", "保存成功!");

        //UserModel.Integral = this.model.Integral;
        //UserModel.IsOnline = this.model.LastIP;
        //UserModel.LastIP = this.model.LastIP;
        //UserModel.LastLogin = this.model.LastLogin;
        //UserModel.loginOutTime = this.model.loginOutTime;
        //UserModel.Logins = this.model.Logins;
        //UserModel.OnlineTime = this.model.OnlineTime;
        //UserModel.Point = this.model.Point;
    }
        
}
